package org.ajax.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ajax.dao.CommdityDAO;
import org.ajax.dao.impl.CommdityDAOImpl;
import org.ajax.entity.Commodity;

import java.io.IOException;
import java.io.PrintWriter;

/*
 *
 *@author XCH
 *@date2024 - 11 - 20
 *
 */
@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        double price = Double.parseDouble(req.getParameter("price"));
        int status = Integer.parseInt(req.getParameter("status"));
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());

        Commodity commodity = new Commodity();
        commodity.setId(id);
        commodity.setName(name);
        commodity.setPrice(price);
        commodity.setStatus(status);
        commodity.setDate(date);
        System.out.println("commodity = " + commodity);
        CommdityDAO dao = new CommdityDAOImpl();
        int result = dao.updateGood(commodity);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (result > 0){
            out.print("{\"code\":200, \"message\":\"商品修改成功\"}");
        }else{
            out.print("{\"code\":500, \"message\":\"商品修改失败\"}");
        }
        out.flush();
    }
}
